package com.lyf.shop.web.controller;

import cn.dsna.util.images.ValidateCode;
import com.lyf.shop.common.Const;
import com.lyf.shop.util.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@WebServlet(value = "/code")
public class CodeController extends BaseController{

    public String checkCode(HttpServletRequest request,HttpServletResponse response){
       String code = request.getParameter("code");
       //厦门的没有值
       String validate = (String) request.getSession().getAttribute(Const.VALIDATE);
       String d = "1";
       if(StrUtils.empty(code)){
           if(validate.equalsIgnoreCase(code)){
               return "0";
           }
       }
       return d;
    }

    public void createCode(HttpServletRequest request, HttpServletResponse response){
        try {
            ValidateCode vc = new ValidateCode(100,40,4,10);
            String code = vc.getCode();
            request.getSession().setAttribute(Const.VALIDATE,code);
            vc.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
